package com.example.twitter;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import twitter4j.Status;
import twitter4j.TwitterException;
import twitter4j.auth.AccessToken;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Tweet extends Activity implements OnTouchListener{
	
	ConfiguracionTwitter config;
	TextView tx;
	Status status;
	ImageView iv;
	TextView textName;
	TextView textUser;
	TextView textDate;
	RelativeLayout rel;
	
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.tweet);
		
		Bundle bundle = this.getIntent().getExtras();
		
		tx = (TextView) this.findViewById(R.id.textViewTweet);
		tx.setTextSize(Long.parseLong("25"));
		textName = (TextView) this.findViewById(R.id.textName);
		textUser = (TextView) this.findViewById(R.id.textScreenName);
		textDate = (TextView) this.findViewById(R.id.textViewDate);
		rel = (RelativeLayout) this.findViewById(R.id.layoutP);
		LayoutInflater inflater = (LayoutInflater) getSystemService(this.getApplicationContext().LAYOUT_INFLATER_SERVICE);
		
		rel.setOnTouchListener(this);
		iv = (ImageView) this.findViewById(R.id.imageUser);
		
		config = new ConfiguracionTwitter(this.getApplicationContext());
		SharedPreferences prefs = this.getSharedPreferences("MySettingsTwitter", 0);
		config.token = prefs.getString("token", "");
		config.tokenSecret = prefs.getString("tokenSecret", "");
		config.accessToken = new AccessToken(config.token, config.tokenSecret);
		
		config.twitter.setOAuthAccessToken(config.accessToken);
		
		
		
		status = config.getTweet(bundle.getLong("id"));
		
		URL url = null;
		
		try {
			url = new URL(status.getUser().getProfileImageURL());
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		InputStream input = null;
		try {
			input = (InputStream) url.getContent();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		Drawable drawable = Drawable.createFromStream(input, null);
		iv.setImageDrawable(drawable);
		
		tx.setText(status.getText());
		
		textName.setText("@" + status.getUser().getScreenName());
		textUser.setText(status.getUser().getName());
		textDate.setText("Enviado " + status.getCreatedAt().toGMTString());
		
		
		
		
		
		
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		menu.add(Menu.NONE, 1, Menu.NONE, "Retwittear");
		
		
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		if(item.getItemId() == 1){
			try {
				config.twitter.retweetStatus(status.getId());
				this.finish();
			} catch (TwitterException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return true;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if(v.getId() == R.id.layoutP){
			Intent intent = new Intent(this, Perfil.class);
			intent.putExtra("id", status.getUser().getId());
			startActivity(intent);
		}
		return true;
	}

	

}
